home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / convert / cvert.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-12-22  |  1.6 KB  |  79 lines

  1. {Conversion Unit -
  2.  
  3.  Designer:  Craig Ward
  4.  Date:      19/7/95
  5.  
  6.  Function:  Measurement converter
  7.  
  8. *******************************************************************************}
  9. unit Cvert;
  10.  
  11. interface
  12.  
  13. uses
  14.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  15.   Forms, Dialogs;
  16.  
  17. type
  18.  TConvertKind = (FahrenConvert, CelsiusConvert, MileConvert, KMConvert);
  19.  TValueConverter = class(TComponent)
  20.   private
  21.     { Private declarations }
  22.     FKind: TConvertKind;
  23.     procedure SetConvertKind(Value: TConvertKind);
  24.   protected
  25.     { Protected declarations }
  26.   public
  27.     { Public declarations }
  28.     function ConvertValue(value: double): double;
  29.   published
  30.     { Published declarations }
  31.     property Kind: TConvertKind read FKInd write SetConvertKind;
  32. end;
  33.  
  34. procedure Register;
  35.  
  36. {****************************************************************************}
  37. implementation
  38.  
  39. {procedure to register this as a custom component}
  40. procedure Register;
  41. begin
  42.   RegisterComponents('Samples', [TValueConverter]);
  43. end;
  44.  
  45. {set the conversion type}
  46. procedure TValueConverter.SetConvertKind(Value: TConvertKind);
  47. begin
  48.  if value <> FKind then
  49.   begin
  50.    FKind := Value;
  51.   end;
  52. end;
  53.  
  54. {convert the value passed as a parameter}
  55. function TValueConverter.ConvertValue(value: double): double;
  56. begin
  57.  case FKind of
  58.   FahrenConvert:
  59.    begin
  60.     result := ((value * 9)/5) + 32;
  61.    end;
  62.   CelsiusConvert:
  63.    begin
  64.     result := ((value - 32) * 5)/9;
  65.    end;
  66.   MileConvert:
  67.    begin
  68.     result := (value/8) * 5;
  69.    end;
  70.   KMConvert:
  71.    begin
  72.     result := (value/5) * 8;
  73.    end;
  74.   end;
  75. end;
  76.  
  77.  
  78. end.
  79.